package com.tanhua.manage.listener;
import com.alibaba.fastjson.JSON;
import com.tanhua.manage.mapper.LogMapper;
import com.tanhua.manage.vo.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "ysx_log_Topic",consumerGroup = "log_group")
@Slf4j
public class LogListener implements RocketMQListener<String> {

    //注入logMapper 往日志表插入数据
    @Autowired
    private LogMapper logMapper;

    /**
     * 日志监听
     * @param message
     */
    @Override
    public void onMessage(String message) {
        log.debug("消息中间件的内容为："+message);
        Map map = JSON.parseObject(message, Map.class);
        //获取userID,type,logTime
        Integer userId = (Integer) map.get("userId");
        String type = (String) map.get("type");
        String logTime = (String) map.get("logTime");
        //创建一个对象
        Log log = new Log();
        log.setUserId(userId.longValue());
        log.setType(type);
        log.setLogTime(logTime);
        log.setEquipment("ysx");
        log.setPlace("bj");
        log.setCreated(new Date());
        //插入数据库
        logMapper.insert(log);

    }
}
